    // pages/staff/customdetail.js
    const app = getApp()
    var CONFIG = require("../../config/config.js");
    var share = require("../../utils/share.js");
    Page({

        /**
         * 页面的初始数据
         */
        data: {
            navarr:[
                {
                        title:'基本信息',
                        type:1,
                },
                {
                        title:'跟进记录',
                        type:2,
                },
                {
                        title:'选课信息',
                        type:3,
                },
                // {
                //         title:'考勤记录',
                //         type:4,
                // }
            ],
            navindex:1,
            crm_id:0,
            infordata:'',
            page:0,
            recorddataall:[],
            courseList:[],
            secfalse:false,
            threefalse:false,
            stuclose:false,
            addlist:[],
            submitfalsephone:false,
            phone:'',
            teamtype:1,
            newconidtype:1,
            newconid:'',
            photo:'',
            imgUrl:CONFIG.API_PIC
        },
        // 拨打电话
        phonetal() {
            var that = this;
            that.setData({
                submitfalsephone: true
            })

        },
        closeclick() {
            var that = this;
            that.setData({
                submitfalsephone: false
            })
        },
        subjectbutphone() {
            var that = this;
            that.setData({
                submitfalsephone: false
            })
            var phone = that.data.phone;
            wx.makePhoneCall({
                phoneNumber: phone //仅为示例，并非真实的电话号码
            })
        },

        messnoneendphone() {
            var that = this;
            that.setData({
                submitfalsephone: false
            })
            var phone = that.data.phone;
            wx.setClipboardData({
                data: phone,
                success: function (res) {
                    wx.showToast({
                        title: '复制成功',
                        icon: 'none',
                        duration: 1500
                    })
                },
                fail: function (res) {
                    wx.showToast({
                        title: '复制失败',
                        icon: 'none',
                        duration: 1500
                    })
                },
            })
        },
        // 点击删除
        detlclick(){
            var that = this;
            that.setData({
                stuclose:true
            })
        },
        chenclose(){
            var that = this;
            that.setData({
                stuclose:false
            })
        },
        hkclose(){
            var that = this;
            wx.showLoading({
            title: '加载中~',
            })
            that.setData({
                stuclose:false
            })
            // share.wxRequest('crmapi/user/stuDetails',{crm_id:that.data.crm_id},function(res){
            //       if(Number(res.data.errcode)==0){

            //       }
            // })
        },
        // 点击选课
        choosecourseall(){
            var that = this;
            var infordata = that.data.infordata;
            var newconidtype = that.data.newconidtype;
            if (Number(newconidtype)==2){
                var newconid = that.data.consultant_id;
            }else{
                var newconid = '';
            }
            wx.navigateTo({
                url: '/pages/staff/choose_course?user_id=' + infordata.user_id + '&tel=' + infordata.phone + '&name=' + infordata.name + '&newconid=' + newconid+'&photo=' + infordata.photo+'',
            })
        },

        // 点击导航切换
        navevclick(e){
            var that = this;
            // console.log(e);
            var type = e.currentTarget.dataset.item.type
            that.setData({
                navindex:type,
                page:0
            })
            if(Number(that.data.navindex)==2){
                // 学员跟进
                that.singleStuRecord();
            }else if(Number(that.data.navindex)==3){
                // 学员的选课记录
                that.userCourseList();
            }
        },

        peoplerecode(){
            // 学员跟进
            that.singleStuRecord();
        },

        /**
        * 生命周期函数--监听页面加载
        */
        onLoad: function (options) {
            var that = this;
            if (options.teamtype){
                var teamtype = options.teamtype
            }else{
                var teamtype = 1;
            }
            if (options.newconidtype) {
                var newconidtype = options.newconidtype;
            } else {
                var newconidtype = 1;
            }
            // newconidtype
            if (Number(newconidtype)==2){
                var newconid = options.consultant_id;
            }else{
                var newconid = 0;
            }
            that.setData({
                crm_id:options.crm_id,
                navindex:options.type,
                teamtype: teamtype,
                consultant_id: options.consultant_id,
                newconidtype: newconidtype,
                newconid: newconid
            })
            // console.log(that.data.teamtype)
            if (Number(newconidtype)==2){
                that.setData({
                    navarr: [
                        {
                            title: '基本信息',
                            type: 1,
                        },
                        {
                            title: '选课信息',
                            type: 3,
                        },
                    ],
                })
            }else if ((Number(options.conCertif) > 1 && Number(options.is_certif) == 0) || options.name==''){
                if (Number(app.globalData.identity) == 3) {
                    that.setData({
                        navarr: [
                            {
                                title: '基本信息',
                                type: 1,
                            },
                        ]
                    })
                }else{
                    that.setData({
                        navarr: [
                            {
                                title: '基本信息',
                                type: 1,
                            },
                            {
                                title: '跟进记录',
                                type: 2,
                            },
                        ]
                    })
                }
            }else{
                if (Number(app.globalData.identity) == 3) {
                    that.setData({
                        navarr: [
                            {
                                title: '基本信息',
                                type: 1,
                            },
                            {
                                title: '选课信息',
                                type: 3,
                            },
                        ],
                    })
                }else{
                    that.setData({
                        navarr: [
                            {
                                title: '基本信息',
                                type: 1,
                            },
                            {
                                title: '跟进记录',
                                type: 2,
                            },
                            {
                                title: '选课信息',
                                type: 3,
                            },
                        ],
                    })
                }
            }
            that.userSubs();
            
            // wx.setStorageSync('followtype',2)
            
        },

        userSubs() {
            wx.showLoading({
                title: '加载中~',
            })
            var that = this;
            share.wxRequest('crmapi/user/userSubs', {}, function (res) {
                if (Number(res.data.errcode) == 0) {
                    var data = res.data.data;
                    // console.log(data);
                    var arr = [];
                    for (var i = 0; i < data.lists.length; i++) {
                        data.lists[i]['valueadd'] = '';
                        arr.push(data.lists[i]);
                    }
                    that.setData({
                        addlist: arr,
                    })
                    that.stuDetails();
                }
                wx.hideLoading()
            })
        },



        // 学员详情
        stuDetails(){
            wx.showLoading({
                title: '加载中~',
            })
            var that = this;
            if (Number(app.globalData.identity) == 3) {
                if (that.data.newconid != '' &&Number(that.data.newconid)>0){
                    var url = 'crmapi/user/stuDetails'
                }else{
                    var url = 'crmapi/Learn/stuDetails'
                }
            }else{
                var url = 'crmapi/user/stuDetails'
            }
            share.wxRequest(url,{crm_id:that.data.crm_id},function(res){
                if(Number(res.data.errcode)==0){
                    var data = res.data.data;
                    that.setData({
                        infordata:data,
                        phone:data.phone
                    })
                    var type = that.data.navindex;
                    if(Number(type)==2){
                        // 学员跟进
                        that.singleStuRecord();
                    }else if(Number(type)==3){
                        // 学员的选课记录
                        that.userCourseList();
                    }
                    // 自定义数据
                    if (data.user_extend){
                        var user_extend = data.user_extend;
                        // console.log(user_extend)
                        if (user_extend != '') {
                            var addlist = that.data.addlist;
                            for (var i = 0; i < addlist.length; i++) {
                                var name = addlist[i].input_name;
                                var input_values = addlist[i].input_values;
                                if (Number(addlist[i].input_type) == 1) {
                                    var uservalue = user_extend[name];
                                    if (!uservalue && (uservalue == '' || Number(uservalue == 0))) {
                                        addlist[i].valueadd = '暂无'
                                    }else{
                                        addlist[i].valueadd = user_extend[name]
                                    }
                                } else if (Number(addlist[i].input_type) == 2) {
                                    var uservalue = user_extend[name];
                                    if (!uservalue && (uservalue == '' || Number(uservalue == 0))) {
                                        addlist[i].valueadd = '暂无';
                                    } else {
                                        for (var j = 0; j < input_values.length; j++) {
                                            if (Number(uservalue) == Number(input_values[j].evalue)) {
                                                addlist[i].valueadd = input_values[j].edesc;
                                                addlist[i].evalue = input_values[j].evalue;
                                            }
                                        }
                                    }
                                } else if (Number(addlist[i].input_type) == 3) {
                                    var uservalue = user_extend[name];
                                    if (!uservalue || (uservalue == '' || Number(uservalue == 0))) {
                                        addlist[i].valueadd = '暂无';
                                    } else {
                                        // var arr = uservalue.split(',');
                                        var arr = uservalue;
                                        // console.log(uservalue)
                                        var str = '';
                                        addlist[i].evalue = uservalue;
                                        for (var j = 0; j < input_values.length; j++) {
                                            for (var z = 0; z < arr.length; z++) {
                                                if (Number(arr[z]) == Number(input_values[j].evalue)) {
                                                    if (str == '') {
                                                        str = input_values[j].edesc;
                                                    } else {
                                                        str = str + ',' + input_values[j].edesc;
                                                    }
                                                    addlist[i].input_values[j].checked = true;
                                                }
                                            }
                                        }
                                        addlist[i].valueadd = str;
                                    }
                                }
                            }
                            that.setData({
                                addlist: addlist,
                            })
                        }
                    }
                }
                wx.hideLoading();
            })
        },

        // 学员跟进
        singleStuRecord(){
            var that = this;
            wx.showLoading({
            title: '加载中~',
            })
            var recorddataall = that.data.infordata;
            var nowpage = that.data.page;
            var obj = {
                crm_id:recorddataall.id,
                nowPage:nowpage,
            }
            share.wxRequest('crmapi/user/singleStuRecord',obj,function(res){
                if(Number(res.data.errcode)==0){
                        var data = res.data.data;
                        // if(Number(that.data.page)<=0){
                        //     that.setData({
                        //         secfalse:false,
                        //     })
                        //     var dataarr = data;
                        // }else{
                        //     if(Number(data.length)>0){
                        //         var dataarr = that.data.recorddataall.concat(data);
                        //     }else{
                        //         var dataarr = that.data.recorddataall
                        //     }
                        // }
                        // that.setData({
                        //     recorddataall:dataarr
                        // })
                }else{
                    var data = [];
                }
                setTimeout(function () {
                    const child = that.selectComponent("#the-recode").showMyTips(data, that.data.page);
                    wx.hideLoading();
                }, 500)
                that.setData({
                    secfalse:true,
                })
                wx.hideLoading();
            })
        },

        // 点击写跟进
        gofollwe(e) {
            var that = this;
            // console.log(type);
            var type = e.detail;
            var infordataall = that.data.infordata;
            wx.navigateTo({
                url: '/pages/staff/stupeople/writecall?crmid='+infordataall.id+'&consult_id='+infordataall.consult_id+'&id='+type+'',
            })
        },

        // 学员的选课记录
        userCourseList(){
            var that = this;
            wx.showLoading({
                title: '加载中~',
            })
            
            var recorddataall = that.data.infordata;
            var nowpage = that.data.page;
            // console.log(nowpage)
            // console.log(recorddataall);
            var obj = {
                uid:recorddataall.user_id,
                consultant_id:recorddataall.consult_id,
                nowPage:nowpage,
            }
            if (Number(app.globalData.identity) == 3) {
                if (that.data.newconid != '' && Number(that.data.newconid) > 0) {
                    var url = 'crmapi/user/userCourseList';
                }else{
                    var url = 'crmapi/Learn/userCourseList';
                }
            }else{
                var url = 'crmapi/user/userCourseList';
            }
            share.wxRequest(url,obj,function(res){
                if(Number(res.data.errcode)==0){
                        var data = res.data.data;
                    
                        // console.log(that.data.page)
                        // if(Number(that.data.page)<=0){
                        //     // console.log(899)
                        //     that.setData({
                        //         threefalse:false,
                        //     })
                        //     var dataarr = data;
                        // }else{
                        //     if(Number(data.length)>0){
                        //             // that.setData({
                        //             //       threefalse:false,
                        //             // })
                        //             var dataarr = that.data.courseList.concat(data);
                        //     }else{
                        //             var dataarr = that.data.courseList
                        //     }
                        // }
                        // that.setData({
                        //     courseList:dataarr
                        // })
                }else{
                    var data = [];
                }
                setTimeout(function () {
                    const child = that.selectComponent("#the-id").showMyTips(data, that.data.page);
                    wx.hideLoading();
                }, 500)
                that.setData({
                    threefalse:true,
                })
            })
        },

        clickpics(){
            var jiaoyanCode = this.data.imgUrl+this.data.infordata.photo;
            share.sendImage(jiaoyanCode);
        },
        /**
         * 生命周期函数--监听页面显示
         */
        onShow: function () {
            var that = this;
            
            if(wx.getStorageSync('followtype')){
                var type = wx.getStorageSync('followtype');
                wx.removeStorageSync('followtype')
                that.setData({
                        navindex:type,
                        page:0
                })
                that.stuDetails();
            }
        },

        /**
         * 生命周期函数--监听页面隐藏
         */
        onHide: function () {

        },

        /**
         * 生命周期函数--监听页面卸载
         */
        onUnload: function () {

        },


        /**
         * 页面上拉触底事件的处理函数
         */
        onReachBottom: function () {
            var that = this;
            that.setData({
                page: that.data.page + 1,
            })
            // console.log(that.data.page)
            if(Number(that.data.navindex)==2){
                // 学员跟进
                that.singleStuRecord();
            }else if(Number(that.data.navindex)==3){
                // 学员的选课记录
                that.userCourseList();
            }
        },

        /**
         * 用户点击右上角分享
         */
        onShareAppMessage: function () {

        }
    })